package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.MediaThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-22
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {
    @Autowired
    private MinIOService minIOService;
    @Override
    public ResponseResult<WmMaterial> upload(MultipartFile file) {
        User user= MediaThreadLocalUtil.get();
//
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        String url = minIOService.upload(file);
        WmMaterial material=new WmMaterial();
        material.setUserId(user.getUserId());
        material.setUrl(url);
        material.setType(0);
        material.setIsCollection(false);
        material.setCreatedTime(new Date());
        this.save(material);
        return ResponseResult.okResult(material);



    }
//这能查询自己的图片
    @Override
    public ResponseResult listByCollection(WmMaterialDto dto) {
        User user = MediaThreadLocalUtil.get();
        IPage<WmMaterial> page=new Page<>(dto.getPage(),dto.getSize());
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        }


        LambdaQueryWrapper<WmMaterial> query=new LambdaQueryWrapper<>();
//       首先根据收藏不收藏进行查询
        if(dto.getIsCollection()!=null){
            query.eq(WmMaterial::getIsCollection,dto.getIsCollection());

        }
//        一个隐藏条件 就是自己只能看自己的wenjian
        query.eq(WmMaterial::getUserId,user.getUserId());
//       排序创建时间降序
        query.orderByDesc(WmMaterial::getCreatedTime);

        IPage<WmMaterial> iPage = this.page(page, query);
        PageResponseResult<List<WmMaterial>> responseResult = new PageResponseResult<>(dto.getPage(), dto.getSize(), iPage.getTotal(),
                iPage.getRecords());
        return responseResult;
    }
//添加收藏
    @Override
    public ResponseResult collection(Integer id) {
        WmMaterial wmMaterial = this.getById(id);
        wmMaterial.setIsCollection(true);
        boolean b = this.updateById(wmMaterial);
        return ResponseResult.okResult(b);
    }
}
